import matplotlib.pyplot as plt
import numpy as np
import os

from numpy.lib.function_base import append

def get_a_file_data(filename, need_key):
    with open(filename, 'r') as f:
        row_list = f.read().splitlines()
    
    for row in row_list:
        key, value = row.split(':')
        if key == need_key:
            return eval(value)

def cr(ave_feas44,ave_feas48,ave_feas52,ave_feas56,ave_feas60,ave_feas64,ave_feas68,ave_feas72,ave_feas76,ave_feas80,ave_feas84,ave_feas88):
    x_data_old = ['EDF', 'SEDF', 'SRPT', 'FB', 'RM', 'SRPT+EDF', 'SRPT-A', 'FIFO', 'FIFO+EDF']
    #line
    plt.figure(figsize=(8,5))
    FIFO_data = []
    FIFO_EDF_data = []
    RM_data = []
    SRPT_A_data = []
    SRPT_data = []
    EDF_data = []
    SEDF_data = []
    SRPT_EDF_data = []
    FB_data = []
    # data_dict = {}
    # ave_feas_data_old = ave_feas40
    # for i in range(len(ave_feas_data_old)):
    #     data_dict[x_data_old[i]] = ave_feas_data_old[i]
    # x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    # ave_48_data = [data_dict[label] for label in x_data]
    # FIFO_data.append(data_dict['FIFO'])
    # FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    # RM_data.append(data_dict['RM'])
    # SRPT_A_data.append(data_dict['SRPT-A'])
    # SRPT_data.append(data_dict['SRPT'])
    # EDF_data.append(data_dict['EDF'])
    # SEDF_data.append(data_dict['SEDF'])
    # SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    # FB_data.append(data_dict['FB'])

    data_dict = {}
    ave_feas_data_old = ave_feas44
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])

    data_dict = {}
    ave_feas_data_old = ave_feas48
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    data_dict = {}
    ave_feas_data_old = ave_feas52
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    


    data_dict = {}
    ave_feas_data_old = ave_feas56
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    
    data_dict = {}
    ave_feas_data_old = ave_feas60
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    
    data_dict = {}
    ave_feas_data_old = ave_feas64
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    data_dict = {}
    ave_feas_data_old = ave_feas68
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    data_dict = {}
    ave_feas_data_old = ave_feas72
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    
    data_dict = {}
    ave_feas_data_old = ave_feas76
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    
    data_dict = {}
    ave_feas_data_old = ave_feas80
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
      
    
    data_dict = {}
    ave_feas_data_old = ave_feas84
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])

    data_dict = {}
    ave_feas_data_old = ave_feas88
    for i in range(len(ave_feas_data_old)):
        data_dict[x_data_old[i]] = ave_feas_data_old[i]
    x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    ave_48_data = [data_dict[label] for label in x_data]
    FIFO_data.append(data_dict['FIFO'])
    FIFO_EDF_data.append(data_dict['FIFO+EDF'])
    RM_data.append(data_dict['RM'])
    SRPT_A_data.append(data_dict['SRPT-A'])
    SRPT_data.append(data_dict['SRPT'])
    EDF_data.append(data_dict['EDF'])
    SEDF_data.append(data_dict['SEDF'])
    SRPT_EDF_data.append(data_dict['SRPT+EDF'])
    FB_data.append(data_dict['FB'])
    
    data_dict = {}
    x_data = ['0.44','0.48','0.52','0.56','0.60','0.64','0.68','0.72','0.76','0.80','0.84','0.88']
    plt.plot(x_data, FIFO_data, ms=6,  marker='x', label="FIFO ", linewidth=2)
    plt.plot(x_data, FIFO_EDF_data, ms=6,  marker='_', label="FIFO-EDF", linewidth=2)
    plt.plot(x_data, RM_data, ms=6, marker='<', label="RM ", linewidth=2)
    plt.plot(x_data, SRPT_A_data, ms=6, marker='+', label="SRPT-A ", linewidth=2)
    plt.plot(x_data, SRPT_data, ms=6,marker='>', label="SRPT ", linewidth=2)
    plt.plot(x_data, EDF_data, ms=6, marker='o', label="EDF", linewidth=2)
    plt.plot(x_data, SEDF_data, ms=6,marker='*',  label="SEDF ", linewidth=2, linestyle = '-')
    plt.plot(x_data, SRPT_EDF_data, ms=6, label="SRPT-EDF ", linewidth=2,linestyle = '-.')
    plt.plot(x_data, FB_data, ms=6, label="Feedback", linewidth=2, linestyle = ':')

    plt.xlabel("Total processor utilization",fontsize = 20)
    plt.ylabel("The fraction of feasibility\n task sets",fontsize = 20)
    
    
    
    
    

    # ave_Comp_ratio_old = ave_vsr
    # data_dict = {}
    # for i in range(len(ave_Comp_ratio_old)):
    #     data_dict[x_data_old[i]] = ave_Comp_ratio_old[i]
    # x_data = ['FIFO','FIFO+EDF','RM','SRPT-A','SRPT','EDF','SEDF','SRPT+EDF','FB']
    # ave_Comp_ratio = [data_dict[label] for label in x_data]
    # plt.plot(x_data, ave_Comp_ratio, color = "plum", marker='*', ms=6, label="ACRC", linewidth=3) # valid slot ratio 


    plt.legend(fontsize = 16)
    # plt.title('CR & SU & VSR', fontsize=18)
    graph_path = os.path.dirname(os.path.abspath(__file__)) + '/graph/'
    if not os.path.isdir(graph_path):
        os.makedirs(graph_path)
    file_name = graph_path + __file__.split('/')[-1].split('.')[0]
    plt.yticks(fontsize=22)
    plt.xticks(fontsize=22,  rotation = 280)
    plt.savefig(f"{file_name}.pdf", bbox_inches='tight')
    plt.close()


if __name__ == '__main__':
    # total_data = []
    # for i in range(40):
    #     res = get_a_file_data(f'../../Schedulability1/4_9_40/test{i}/result_data.log', 'average cr')
    #     total_data.append(res)
    #     np_total_data = np.array(total_data)
    #     print(res)
    # ave_feas40 = np_total_data.mean(axis = 0).tolist()
    # print("ave_feas40",ave_feas40)


    total_data = []
    for i in range(40):
        res = get_a_file_data(f'../../Schedulability1/4_9_44/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas44 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas44",ave_feas44)

    total_data = []
    for i in range(40):
        res = get_a_file_data(f'../../Schedulability2/4_9_48/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas48 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas48",ave_feas48)

    # total_data = []
    # for i in range(10):
    #     res = get_a_file_data(f'../../Schedulability/4_9_48/test{i}/result_data.log', 'average cr')
    #     total_data.append(res)
    #     print(res)
    # for i in range(30):
    #     res = get_a_file_data(f'../../Schedulability1/4_9_48/test{i}/result_data.log', 'average cr')
    #     total_data.append(res)
    #     np_total_data = np.array(total_data)
    #     print(res)

    # ave_feas48 = np_total_data.mean(axis = 0).tolist()
    # print("ave_feas48",ave_feas48)

    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_52/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        # np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_52/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas52 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas52",ave_feas52)

    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_56/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        # np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_56/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas56 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas56",ave_feas56)
    
    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_60/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        # np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_60/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas60 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas60",ave_feas60)
    
    total_data = []

    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_64/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_64/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas64 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas64",ave_feas64)
    
    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_68/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_68/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas68 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas64",ave_feas68)

    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_72/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_72/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas72 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas72",ave_feas72)


    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_76/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_76/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas76 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas76",ave_feas76)

    total_data = []
    for i in range(10):
        res = get_a_file_data(f'../../Schedulability/4_9_80/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    for i in range(30):
        res = get_a_file_data(f'../../Schedulability1/4_9_80/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas80 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas80",ave_feas80)
    
    total_data = []
    for i in range(34):
        res = get_a_file_data(f'../../Schedulability1/4_9_84/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas84 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas84",ave_feas84)

    total_data = []
    for i in range(40):
        res = get_a_file_data(f'../../Schedulability1/4_9_88/test{i}/result_data.log', 'average cr')
        total_data.append(res)
        np_total_data = np.array(total_data)
        print(res)
    ave_feas88 = np_total_data.mean(axis = 0).tolist()
    print("ave_feas88",ave_feas88)

    cr( ave_feas44,ave_feas48,ave_feas52,ave_feas56,ave_feas60, ave_feas64,ave_feas68,ave_feas72,ave_feas76,ave_feas80,ave_feas84,ave_feas88)
        
